package br.ufrgs.tst.ga;

import java.util.List;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

import br.ufrgs.ga.TSPGeneticAlgorithm;
import br.ufrgs.ga.exceptions.AlgorithmException;
import br.ufrgs.ga.model.Population;
import br.ufrgs.tsp.model.ICity;
import br.ufrgs.tst.TestHelper;

public class TestGeneticAlgorithm {

	private TSPGeneticAlgorithm geneticAlgorithm;
	
	@Before
	public void setUp() {
		// Initial cities configuration for a TSP instance.
		List<ICity> citiesConfiguration = TestHelper.getCitiesConfigurationStub();
		//citiesConfiguration.add
		geneticAlgorithm = new TSPGeneticAlgorithm(citiesConfiguration);
	}
	
	@Test
	public void executeTest() throws AlgorithmException {
		geneticAlgorithm.execute();
		System.out.println("The first valid TSP tour found is: " + geneticAlgorithm.getBestIndividualSoFar());
	}
	
	@Test
	public void initializePopulationTest() {
		geneticAlgorithm.initializePopulation();
		Assert.assertTrue(geneticAlgorithm.getPopulation().size() == Population.POPULATION_SIZE);
	}
	
	@Test
	public void fitnessCalculationTest() {
		geneticAlgorithm.initializePopulation();
		geneticAlgorithm.fitnessCalculation();
	}
	
	@Test
	public void selectParentsToReproductionTest() {
		geneticAlgorithm.initializePopulation();
		geneticAlgorithm.fitnessCalculation();
		
		Assert.assertTrue(geneticAlgorithm.getSelectedIndividualsToReproduction().size() == 0);
		geneticAlgorithm.selectParentsToReproduction();
		Assert.assertTrue(geneticAlgorithm.getSelectedIndividualsToReproduction().size() == Population.REPRODUCTION_FREQUENCE);
	}
	
	@Test
	public void reproductionTest() {
		geneticAlgorithm.initializePopulation();
		geneticAlgorithm.fitnessCalculation();
		geneticAlgorithm.selectParentsToReproduction();
		
		geneticAlgorithm.reproduction();
		Assert.assertTrue(geneticAlgorithm.getPopulation().size() == Population.POPULATION_SIZE);
	}
	
	@Test
	public void mutationTest() {
		geneticAlgorithm.initializePopulation();
		geneticAlgorithm.fitnessCalculation();
		geneticAlgorithm.selectParentsToReproduction();
		geneticAlgorithm.reproduction();
		geneticAlgorithm.mutation();
	}
	
	@Test
	public void solutionFoundTest() {
		geneticAlgorithm.initializePopulation();
		geneticAlgorithm.solutionFound();
	}
}
